<?php

namespace App\Admin\Controllers;

use App\Models\CaseBatch;
use App\Models\CaseCustomer;
use App\Models\CaseContract;
use App\Admin\Renderable\CaseCustomerTable;
use App\Admin\Renderable\ContractTable;
use App\Models\CaseProduct;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Admin\Traits\Customfields;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseBatchController extends AdminController
{
    use Customfields;
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CaseBatch(), function (Grid $grid) {
            $grid->column('number', '批次号')->sortable();
            $grid->column('case_customer_id')->display(function ($userId) {
                return CaseCustomer::find($userId)->name;
            });
            $grid->column('case_contract_id')->display(function ($contractId) {
                return CaseContract::find($contractId)->title;
            });
            $grid->column('case_product_id')->display(function ($productId) {
                return optional(CaseProduct::find($productId))->name;
            });
            $this->gridfield($grid, 'batch');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CaseBatch(), function (Show $show) {
            $show->field('id');
            $show->field('case_customer_id');
            $show->field('case_contract_id');
            $show->field('case_product_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CaseBatch(), function (Form $form) {
            $form->display('id');
            $form->text('number', '批次号')->help('如不填写，则系统会自动生成委托批次号');
            $form->selectTable('case_customer_id')
                ->title('选择所属客户')
                ->dialogWidth('50%') // 弹窗宽度，默认 800px
                ->from(CaseCustomerTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->model(CaseCustomer::class, 'id', 'name'); // 设置编辑数据显示

            $form->selectTable('case_contract_id')
                ->title('选择当前发票所属合同')
                ->dialogWidth('50%') // 弹窗宽度，默认 800px
                ->from(ContractTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->model(CaseContract::class, 'id', 'title'); // 设置编辑数据显示

            $form->select('case_product_id', '签约产品')->options(CaseProduct::pluck('name', 'id'))->required();
            $this->formfield($form, 'batch');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $customer = CaseCustomer::find($form->case_customer_id);
                    $batch_count = $customer->batch_count + 1;
                    if (!$form->number) {
                        $number = $customer->id. '-' . $batch_count . '-' . date("Ymd");
                        $form->number = $number;
                    }
                    $customer->update(['batch_count' => $batch_count]);
                }
                return;
            });
        });
    }
}
